'use strict';
const Service = require('egg').Service;
const queryString = require('querystring');
const lodash = require('lodash');

class PaginatorService extends Service {

  /*
      * 分页 - bootstrap
      * @param int total 数据总条数
      * @param int currentPage 当前页
      * @param string url 链接
      * @param int lastPage 总页数
      * return string
      * */
  async paginator(total, currentPage, url, lastPage) {
    if (!total) return '';
    url = url ? url : this.ctx.request.path; // /admin/index/index
    const { pageSize } = this.config.common;
    // let pageSize = 10;
    // 参数操作
    const query = this.ctx.request.query; // {a:1,b:2}
    if (query._csrf) lodash.unset(query, '_csrf');

    lastPage = lastPage ? parseInt(lastPage) : Math.ceil(total / pageSize);
    if (lastPage < 2) return '';
    currentPage = currentPage ? parseInt(currentPage) : 1;
    let html = '<ul class="pagination">';
    if (currentPage === 1) {
      html += '<li class="disabled"><span>首页</span></li>';
      html += '<li class="disabled"><span>上一页</span></li>';
    } else {
      query.page = 1;
      html += '<li><a href="' + url + '?' + queryString.stringify(query) + '">首页</a></li>';
      query.page = parseInt(currentPage) - 1;
      html += '<li><a href="' + url + '?' + queryString.stringify(query) + '">上一页</a></li>';
    }
    // 是否在10页以内
    if (lastPage <= 10) {
      for (let i = 1; i < lastPage + 1; i++) {
        if (i === currentPage) {
          html += '<li class="active"><span>' + i + '</span></li>';
        } else {
          query.page = i;
          html += '<li><a href="' + url + '?' + queryString.stringify(query) + '">' + i + '</a></li>';
        }
      }
    } else {
      // 非10页内，但当前页在6页内
      if (currentPage < 6) {
        for (let i = 1; i < 11; i++) {
          if (i === currentPage) {
            html += '<li class="active"><span>' + i + '</span></li>';
          } else {
            query.page = i;
            html += '<li><a href="' + url + '?' + queryString.stringify(query) + '">' + i + '</a></li>';
          }
        }
      } else if (currentPage > lastPage - 6) { // 在后五页内
        for (let i = lastPage - 10; i < lastPage + 1; i++) {
          if (i === currentPage) {
            html += '<li class="active"><span>' + i + '</span></li>';
          } else {
            query.page = i;
            html += '<li><a href="' + url + '?' + queryString.stringify(query) + '">' + i + '</a></li>';
          }
        }
      } else {
        for (let i = parseInt(currentPage) - 5; i < currentPage; i++) {
          query.page = i;
          html += '<li><a href="' + url + '?' + queryString.stringify(query) + '">' + i + '</a></li>';
        }
        html += '<li class="active"><span>' + currentPage + '</span></li>';
        // let end_page = ;
        for (let i = parseInt(currentPage) + 1; i < parseInt(currentPage) + 6; i++) {
          query.page = i;
          html += '<li><a href="' + url + '?' + queryString.stringify(query) + '">' + i + '</a></li>';
        }
      }
    }
    if (currentPage === lastPage) {
      html += '<li class="disabled"><span>下一页</span></li>';
      html += '<li class="disabled"><span>末页</span></li>';
    } else {
      query.page = parseInt(currentPage) + 1;
      html += '<li><a href="' + url + '?' + queryString.stringify(query) + '">下一页</a></li>';
      query.page = lastPage;
      html += '<li><a href="' + url + '?' + queryString.stringify(query) + '">末页</a></li>';
    }
    html += '</ul>';
    return html;
  }

  pageHtmlReplace(html, reg_list = []) {
    if (reg_list.length !== 4) reg_list = [ '«', '&lt;', '&gt;', '»' ];
    const old_reg = [ '首页', '上一页', '下一页', '末页' ];
    for (const k in reg_list) {
      html = html.replace(old_reg[k], reg_list[k]);
    }
    return html;
  }

  async paginatorReplace(total, currentPage, url, lastPage, reg_list = []) {
    const html = await this.paginator(total, currentPage, url, lastPage);
    return this.pageHtmlReplace(html, reg_list);
  }


}

module.exports = PaginatorService;
